package com.jiaming.restaurant.api.trade.controller;

import cn.hutool.core.util.StrUtil;
import com.jiaming.restaurant.api.common.bean.contants.CommonContants;
import com.jiaming.restaurant.api.common.bean.vo.MyPageVO;
import com.jiaming.restaurant.api.common.response.ResultCode;
import com.jiaming.restaurant.api.common.response.ResultCodeEnum;
import com.jiaming.restaurant.api.common.response.ResultVo;
import com.jiaming.restaurant.api.trade.bean.entity.Trade;
import com.jiaming.restaurant.api.trade.bean.vo.*;
import com.jiaming.restaurant.api.trade.service.ITradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author dragon
 * @create 2021-02-24 16:29
 */
@RestController
@RequestMapping(value = "/trade")
public class TradeController {

    @Autowired
    ITradeService tradeService;

    @PostMapping(value = "/save")
    public ResultVo<TradeSaveDataVo> save(@RequestBody @Valid TradeVo tradeVo) {

        //参数校验
//        return new ResultVo<>(ResultCodeEnum.FAIL_PARAM_MISSING);

        TradeSaveDataVo tradeSaveDataVo = tradeService.saveTrade(tradeVo);
        //封装响应数据返回给前台
        return new ResultVo<>(ResultCodeEnum.SUCCESS,tradeSaveDataVo);
    }

    @PostMapping(value = "add_goods")
    public ResultVo<TradeSaveDataVo> addGoods(@RequestBody AddGoodsVo addGoodsVo) {


//        return new ResultVo<>(ResultCodeEnum.FAIL_PARAM_MISSING);

        TradeSaveDataVo tradeSaveDataVo = tradeService.addTrade(addGoodsVo);

        return new ResultVo<>(ResultCodeEnum.SUCCESS,tradeSaveDataVo);
    }

    @PostMapping(value = "/info")
    public ResultVo<TradeInfoVo> info(@RequestParam(value = "trade_id") String tradeId){
        Map<String, Object> result = new HashMap<>();
        //安全效验
        if(StrUtil.isEmpty(tradeId)){

        }
        //调用service获取订单数据
        TradeInfoVo tradeInfoVo = tradeService.getByTradeId(tradeId);
        //封装响应的数据返回给前端
        return new ResultVo<>(ResultCodeEnum.SUCCESS,tradeInfoVo);

    }

    @PostMapping(value = "/pay")
    public ResultVo<TradeSaveDataVo> pay(@RequestParam(value = "trade_id") String tradeId,
                      @RequestParam Integer amount){
        Map<String, Object> result = new HashMap<>();
        //安全效验
        if(StrUtil.isEmpty(tradeId) || amount <= 0){
            return new ResultVo<>(ResultCodeEnum.FAIL_PARAM_MISSING);
        }
        //调用service处理订单业务
        TradeSaveDataVo dataVo = tradeService.pay(tradeId, amount);

        //封装响应数据返回给前端
        return new ResultVo<>(ResultCodeEnum.SUCCESS,dataVo);
    }

    @PostMapping(value = "/page")
    public ResultVo<MyPageVO> page(@RequestBody TradePageVo tradePgeVo){
        Map<String, Object> result = new HashMap<>();
        //安全校验
        if(StrUtil.isEmpty(tradePgeVo.getUserId())){
            result.put("code", ResultCode.FAIL_MISSING_PARAM);
            result.put("msg", "缺少必填参数");

        }
        if(tradePgeVo.getPageNum() < 0){
            tradePgeVo.setPageNum(1L);
        }
        if(tradePgeVo.getPageSize() < CommonContants.MIN_PAGE_SIZE){
            tradePgeVo.setPageSize(CommonContants.MIN_PAGE_SIZE);
        }

        //调用service处理订单业务
        MyPageVO<Trade> data = tradeService.pageDetail(tradePgeVo);

        //封装响应数据返回给前端

        return new ResultVo<>(ResultCodeEnum.SUCCESS,data);
    }
}
